<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterKey"> </el-input>
    <el-tree
      :data="directions"
      :props="defaultProps"
      @node-click="nodeClickHandler"
      :filter-node-method="filterNode"
      ref="tree"
      :accordion="false"
      :default-expand-all="true"
      :highlight-current="false"
    >
    </el-tree>
  </div>
</template>

<script>
export default {
  props: {
    directions: {
      type: Array,
      required: true,
    },
  },
  watch: {
    filterKey(val) {
      this.$refs.tree.filter(val);
    },
  },
  data() {
    return {
      filterKey: '',
      defaultProps: {
        label: 'title',
        children: 'directions',
      },
    };
  },
  methods: {
    nodeClickHandler(direction) {
      console.log(direction);
      this.filterKey = '';
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.title.indexOf(value) !== -1;
    },
  },
};
</script>

<style></style>
